const UserHttp = require("../../models/userHttp")
Page({
  data:{
    isAuth:false,
    logo:"",
    username:"",
    movies:""
  },
  onLoad:function(options){
    this.getDefaultHttp();
    var self = this;
    wx.getSetting({
      success(res){
        if(res.authSetting['scope.userInfo']){
          //已经授权,可以直接使用getUserInfo获取头像昵称
          wx.getUserInfo({
            success: function(res){
              var userInfo = res.userInfo;
              var {avatarUrl,nickName} = userInfo;
              self.setData({
                isAuth:true,
                logo:avatarUrl,
                username:nickName
              })
            },
          })
        }
      }
    })
  },
  bindGetUserInfo(e){
    var userInfo = e.detail.userInfo;
    //点击授权才会确定获取用户信息
    if(userInfo){
      var {avatarUrl,nickName} = userInfo;
      this.setData({
        isAuth:true,
        logo:avatarUrl,
        username:nickName
      })
    }
  },
  async onChange(event){
    var name = event.detail.name;
    var result;
    if(name == "getCollect"){
      var res = await UserHttp.getCollectMovie()
      result = res.data.res
    }else{
      var res = await UserHttp.getHistoryMovie()
      result = res.data.res
    }
    this.setData({movies:result})
  },
  async getDefaultHttp(){
    var res = await UserHttp.getCollectMovie()
    var result = res.data.res
    this.setData({movies:result})
  }
  
})